package it.uniroma2.art.owlart.langsearch;

import it.uniroma2.art.owlart.langsearch.index.config.StandardAnalysisConfiguration;
import it.uniroma2.art.owlart.langsearch.search.OntIndexSearcher;
import it.uniroma2.art.owlart.model.ARTURIResource;
import it.uniroma2.art.owlart.models.impl.URIResourceIteratorFilteringResourceIterator;
import it.uniroma2.art.owlart.navigation.ARTResourceIterator;
import it.uniroma2.art.owlart.navigation.ARTURIResourceIterator;
import it.uniroma2.art.owlart.protegeimpl.factory.ARTModelFactoryProtegeImpl;
import it.uniroma2.art.owlart.protegeimpl.models.OWLModelProtegeImpl;
import it.uniroma2.art.owlart.protegeimpl.start.AGROVOC_INFO;

import java.util.Iterator;

import org.apache.lucene.util.Version;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * This class accesses MySQL-based AGROVOC ontology. It can be used to produce the CUSTOM indexes which are
 * created from instances of C_NOUN, according to the particular multilingual knowledge organization of first
 * OWL version of AGROVOC ontology<br/>
 * In addition to this, this class contains explicit Protege API code, which is used to manipulate results
 * which are returned by the OntologyIndexer in terms of OWL ART API.
 * 
 * 
 * @author Armando Stellato <stellato@info.uniroma2.it>
 * 
 */
public class AGROVOC_CUSTOMSEARCH_ProtegeDB_plusProtegeAPI_TestOld {

	protected static Logger logger = LoggerFactory
			.getLogger(AGROVOC_CUSTOMSEARCH_ProtegeDB_plusProtegeAPI_TestOld.class);

	protected static String genericAIMSBaseNS = "http://aims.fao.org/aos/";
	//used for all AGROVOC specific domain concepts
	protected static String agrovocBaseNS = "http://aims.fao.org/aos/agrovoc/";
	//application concepts used by CONCEPT SERVER 1.0
	protected static String commonAIMBaseNS = "http://aims.fao.org/aos/common/";

	protected static String dbusername = "root";
	protected static String dbpwd = "mysqlstarred";

	public static void main(String[] args) {
		try {

			String chosenDB = AGROVOC_INFO.current_agrovoc_small_DB;

			ARTModelFactoryProtegeImpl fact = new ARTModelFactoryProtegeImpl();

			OWLModelProtegeImpl model = (OWLModelProtegeImpl) fact.loadOWLModel("com.mysql.jdbc.Driver",
					"jdbc:mysql://127.0.0.1:3306/" + chosenDB
							+ "?requireSSL=false&useUnicode=true&characterEncoding=UTF-8", chosenDB,
					dbusername, dbpwd);

			String indexLocation = "./data/index";


			StandardAnalysisConfiguration conf = new StandardAnalysisConfiguration(Version.LUCENE_36, "en", "it");
			conf.setLanguagesOfURILocalNames("en", "it");
			OWLSearchEngine oi = new OWLSearchEngine(model, indexLocation, conf);
		

			String customIndexName = "c_nounInstances";

			
			/*
			 * just a check for actual namespaces of available classes 
			ARTURIResource owlCls = model.createURIResource(OWL.NAMESPACE + "Class");
			ARTResourceIterator testIt = model.listInstances(owlCls, false);
			while (testIt.streamOpen()) {
				System.out.println(testIt.getNext().asURIResource().getURI());
			}
			*/
			
			// INDEX BUILDING (decomment this to redo indexing)
			
			System.out.println("building index at: " + indexLocation);
			ARTURIResource cnoun = model.createURIResource(commonAIMBaseNS + "c_noun");
			ARTURIResourceIterator it = new URIResourceIteratorFilteringResourceIterator(model.listInstances(
					cnoun, false));
			oi.buildCustomIndex(customIndexName, it, true);
			logger.info("INDEX BUILT");
			

			oi.createOntIndexSearcher();
			System.out.println("ontsearcher armed");

			Iterator<ARTURIResource> retrievedResources;
			Iterator<String> retrievedLabels;


			// *************************************
			// example over AGROVOC ontology
			// *************************************

			oi.setTHRESHOLD(0.5);

			
			retrievedLabels = oi.findLabelsForIndex(customIndexName, "effetti", "it",
					OntIndexSearcher.FUZZY_MATCH).iterator();
			while (retrievedLabels.hasNext())
				System.out.println(retrievedLabels.next());
			
			retrievedResources = oi.findResourcesForIndex(customIndexName, "effetti", "it",
					OntIndexSearcher.FUZZY_MATCH).iterator();
			while (retrievedResources.hasNext()) {
				ARTURIResource foundURIResource = retrievedResources.next();
				System.out.println("found uriResource for cname: " + foundURIResource);
				String resName = model.getProtegeOWLModel().getResourceNameForURI(foundURIResource.getURI());
				System.out.println("found class ProtegeName: " + resName);
				System.out.println("found protege class: "
						+ model.getProtegeOWLModel().getOWLNamedClass(resName));
			}
			
			retrievedResources = oi.findResourcesForIndex(customIndexName, "deer", "en",
					OntIndexSearcher.EXACT_MATCH).iterator();
			while (retrievedResources.hasNext()) {
				ARTURIResource foundURIResource = retrievedResources.next();
				System.out.println("found uriResource for cname: " + foundURIResource);
				String resName = model.getProtegeOWLModel().getResourceNameForURI(foundURIResource.getURI());
				System.out.println("found class ProtegeName: " + resName);
				System.out.println("found protege class: "
						+ model.getProtegeOWLModel().getOWLNamedClass(resName));
			}

			retrievedResources = oi.findResourcesForIndex(customIndexName, "deer", "en",
					OntIndexSearcher.EXACT_MATCH).iterator();
			while (retrievedResources.hasNext()) {
				ARTURIResource foundCNameURIResource = retrievedResources.next();
				// String foundCNameURIResourceString =
				// model.getProtegeOWLModel().getResourceNameForURI(foundCNameURIResource.getURI());
				ARTURIResource hasLexProp = model.createURIResource(agrovocBaseNS + "hasLexicalization");

				ARTResourceIterator associatedDomainObjects = model.listSubjectsOfPredObjPair(hasLexProp,
						foundCNameURIResource, true);
				URIResourceIteratorFilteringResourceIterator urifilt = new URIResourceIteratorFilteringResourceIterator(
						associatedDomainObjects);
				while (urifilt.hasNext()) {
					ARTURIResource domainObject = urifilt.next();
					logger.info("domainObject: " + domainObject);
					String domainObjectName = model.getProtegeOWLModel().getResourceNameForURI(
							domainObject.getURI());
					logger.info("domain object ProtegeName: " + domainObjectName);
					logger.info("found protege individual: "
							+ model.getProtegeOWLModel().getOWLIndividual(domainObjectName));
					ARTResourceIterator associatedDomainClasses = model.listTypes(domainObject, false);
					URIResourceIteratorFilteringResourceIterator classesURIfilt = new URIResourceIteratorFilteringResourceIterator(
							associatedDomainClasses);
					while (classesURIfilt.hasNext()) {
						ARTURIResource domainClass = classesURIfilt.next();
						logger.info("domainClass: " + domainClass);
						String domainClassName = model.getProtegeOWLModel().getResourceNameForURI(
								domainClass.getURI());
						logger.info("domain class ProtegeName: " + domainClassName);
					}

				}
			}

		} catch (Exception e) {
			e.printStackTrace(System.err);
			logger.error(e.getMessage());

			System.err.flush();
			System.err.println("error");

			e.printStackTrace();
		}
	}

}
